package cn.com.ddapp.ddm.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.AddressAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.mine.Addresses;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.RecycleViewDivider;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import cn.com.ddapp.ddm.widget.TipsToast;
import okhttp3.Call;

public class AddressActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    AddressAdapter adapter;
    @BindView(R.id.address_empty)
    EmptyLayout empty;
    @BindView(R.id.noaddress)
    TextView noaddress;
    private String userId;

    private List<Addresses.ListBean> list;

    @Override
    public void initView() {

        setTitle(R.string.address_management);
        setTitleLeftImg(R.mipmap.back_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(list.size() <= 0){
                    setNull();
                }
                finish();
            }
        });
        userId = SPUtils.getStringData(AddressActivity.this, SPUtils.USERID);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        //添加分割线
        recyclerView.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, 10, getResources().getColor(R.color.bg_color)));
        RequestData();
    }

    @Override
    public void initData() {
    }

    public void RequestData() {
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            empty.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback(empty) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    Addresses addresse = gson.fromJson(response, Addresses.class);
                    if (addresse.getResult().equals("OK")) {
                        list = addresse.getList();
                        if(list.size()>0){
                            SPUtils.setStringData(AddressActivity.this,SPUtils.REFID,list.get(0).getRefId()+"");
                        }
                        adapter = new AddressAdapter(AddressActivity.this, addresse.getList(),TDevice.isTablet(AddressActivity.this));
                        recyclerView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        if (adapter != null) {
                            adapter.setOnItemClickListener(new AddressAdapter.OnRecyclerViewItemClickListener() {
                                @Override
                                public void onItemClick(View view, final int i, AddressAdapter.ItemViewHolder holder) {
                                    String flag = getIntent().getStringExtra("flag");
                                    if (flag.equals("orderSure")) {
                                        //下单点击返回
                                        Intent in = new Intent();
                                        in.putExtra("name", list.get(i).getName());
                                        in.putExtra("phone", list.get(i).getPhone());
                                        in.putExtra("address", list.get(i).getProvince()
                                                + list.get(i).getCity()+list.get(i).getCounty()
                                                + list.get(i).getAddress());
                                        in.putExtra("refId",list.get(i).getRefId()+"");

                                        OkUtilCallback callback = new OkUtilCallback() {
                                            @Override
                                            public void onMyResponse(String response, int id) {
                                                Gson gson = new Gson();
                                                Entity result = gson.fromJson(response, Entity.class);
                                                if (result.getResult().equals("OK")) {
                                                    SPUtils.setStringData(AddressActivity.this,SPUtils.REFID,list.get(i).getRefId()+"");
                                                }
                                            }
                                        };
                                        OKUtilAPI.post(OKUtilAPI.setDefaultAddress,"userId="+userId+"&id="+list.get(i).getId(),callback);

                                        setResult(Activity.RESULT_OK, in);
                                        finish();
                                    } else {
                                        //个人中心点击返回
                                    }
                                }
                            });
                        }
                    } else {
                        TipsToast.makeText(AddressActivity.this, addresse.getResult(), TipsToast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    empty.setErrorType(EmptyLayout.NETWORK_ERROR);
                    super.onError(call, e, id);
                }
            };
            String paramsStr = "userId=" + userId;
            if(TDevice.isTablet(AddressActivity.this)){
                paramsStr = paramsStr + "&deviceType=ATM";
            }
            OKUtilAPI.post(OKUtilAPI.getAddress, paramsStr, callback);
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_address;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        RequestData();
    }

    @OnClick({R.id.img_error_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_error_layout:
                RequestData();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if(list.size() <= 0){
            setNull();
        }
        super.onBackPressed();
    }

    private void setNull() {
        Intent in = new Intent();
        in.putExtra("name", "");
        in.putExtra("phone", "");
        in.putExtra("address", "");
        in.putExtra("refId","");
        setResult(Activity.RESULT_OK, in);
    }
}
